package com.google.code.krillion.firehose.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.logging.Logger;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.google.appengine.api.labs.taskqueue.Queue;
import com.google.appengine.api.labs.taskqueue.QueueFactory;
import static com.google.appengine.api.labs.taskqueue.TaskOptions.Builder.*;


public class PacificTimeZoneServlet implements Servlet {
    private static final Logger log = Logger.getLogger(PacificTimeZoneServlet.class.getName());
	private static ServletConfig config;
	private static ArrayList<String> zipList;
    private static final int ZIP_LIST_DEFAULT_SIZE = 8;
    private static final String PACIFIC_ZIP_PROP = "pacific.zip.";  
	private static ArrayList<String> categoryList;
    private static final int CATEGORY_LIST_DEFAULT_SIZE = 3;
    private static final String PRODUCT_CATEGORY_PROP = "product.category.";  

	@Override
	public void destroy() {
		config = null;
	}

	@Override
	public ServletConfig getServletConfig() {
		return config;
	}

	@Override
	public String getServletInfo() {
		return null;
	}

	@Override
	public void init(ServletConfig sc) throws ServletException {
		config = sc;
		ResourceBundle bundle = ResourceBundle.getBundle("com.google.code.krillion.firehose.server.krillion");
		int i = 1;
		String zip;
		zipList = new ArrayList<String>(ZIP_LIST_DEFAULT_SIZE);
		try {
			while ((zip = (String) bundle.getObject(PACIFIC_ZIP_PROP + i)) != null) {
		        log.info(PACIFIC_ZIP_PROP + i + " = " + zip);
				zipList.add(zip);
				i++;
			}
		} catch (MissingResourceException mre) {
			log.info("Init: Successfully loaded " + --i + " PST zip codes");
		}
		if (i < ZIP_LIST_DEFAULT_SIZE) {
			log.severe("Init: Failed to load at least " + ZIP_LIST_DEFAULT_SIZE + " PST zip codes");
		}			
		
		i = 1;
		String cat;
		categoryList = new ArrayList<String>(CATEGORY_LIST_DEFAULT_SIZE);
		try {
			while ((cat = (String) bundle.getObject(PRODUCT_CATEGORY_PROP + i)) != null) {
		        log.info(PRODUCT_CATEGORY_PROP + i + " = " + cat);
				categoryList.add(cat);
				i++;
			}
		} catch (MissingResourceException mre) {
			log.info("Init: Successfully loaded " + --i + " product categories");
		}
		if (i < CATEGORY_LIST_DEFAULT_SIZE) {
			log.severe("Init: Failed to load at least " + CATEGORY_LIST_DEFAULT_SIZE + " product categories");
		}
	}

	@Override
	public void service(ServletRequest req, ServletResponse resp)
			throws ServletException, IOException {
        Queue queue = QueueFactory.getQueue("products");
		resp.setContentType("text/plain");
		PrintWriter writer = resp.getWriter();
		for (String zip : zipList) {
			for (String category : categoryList) {
		        queue.add(url("/products").param("zip", zip).param("category", category));
		        writer.println("added zip/" + zip + " cat/" + category + " combination to products queue \n");
			}
	        writer.flush();
		}
		writer.close();
	}

}
